<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="学生名称" prop="studentName">
        <el-input v-model="queryParams.counsellorName" placeholder="请输入学生名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="queryParams.type" placeholder="请选择请假类型">
          <el-option v-for="dict in dict.type.edu_leave_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择请假状态">
          <el-option v-for="dict in dict.type.edu_leave_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker type="datetimerange" v-model="beginTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" clearable></el-date-picker>
        </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
          <el-date-picker type="datetimerange" v-model="endTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" clearable></el-date-picker>
        </el-form-item>
      <el-form-item label="批准人名称" prop="ratifyName">
        <el-input v-model="queryParams.ratifyName" placeholder="请输入批准人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="success" plain size="mini" @click="handleApply" v-hasPermi="['edu:leave:apply']">请假申请</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="leaveList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="主键id" align="center" prop="askLeaveId" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="辅导员名称" align="center" prop="counsellorName" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.beginTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="请假类型" align="center" prop="typeName" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="请假原因" align="center" prop="reason" />
      <el-table-column label="批准时间" align="center" prop="ratifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.ratifyTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="批准人名称" align="center" prop="ratifyName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" class-name="small-padding fixed-width">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleShowDetail(scope.row)" v-hasPermi="['edu:leave:approve']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <el-dialog title="请假申请" :visible.sync="applyDialogFormVisible" width="500px" append-to-body center :before-close="handleClose" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="学生姓名" :disabled="dialogType" />
        </el-form-item>
        <el-form-item label="辅导员" prop="counsellorName">
          <el-input v-model="form.counsellorName" placeholder="辅导员姓名" :disabled="dialogType" />
        </el-form-item>
        <el-form-item label="请假类型">
          <el-select v-model="form.type" placeholder="请选择请假类型" :disabled="dialogType">
            <el-option v-for="dict in dict.type.edu_leave_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" v-if="dialogType">
          <el-select v-model="form.status" placeholder="状态" :disabled="dialogType">
            <el-option v-for="dict in dict.type.edu_leave_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请假时间" prop="leaveTime">
          <el-date-picker type="datetimerange" v-model="leaveTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" clearable :disabled="dialogType"></el-date-picker>
        </el-form-item>
        <el-form-item label="请假原因">
          <el-input type="textarea" v-model="form.reason" placeholder="请输入请假原因" :disabled="dialogType"></el-input>
        </el-form-item>

        <el-form-item v-if="form.status == 1">
          <el-button type="primary" @click="handleAppreve(form.askLeaveId,2)">同意</el-button>
          <el-button type="primary" @click="handleAppreve(form.askLeaveId,3)">拒绝</el-button>
          <!-- <el-button type="primary" @click="submitForm" v-if="!dialogType">提交申请</el-button> -->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>  

  <script>  
import { apply, listCounsellor, approve, addLeave, revoke } from "@/api/edu/leave";
export default {
  //字典
  dicts: ['edu_leave_type', 'edu_leave_status'],
  data () {

    return {
      form: {
        beginTime: '',
        endTime: '',
        reason: '',
        type: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        counsellorName: null,
        beginTime: null,
        beginStartTime:null,
        beginStopTime:null,
        endTime: null,
        endStartTime: null,
        endStopTime: null,
        status: null,
        ratifyTime: null,
        type: null,
      },
      showSearch: true, //显示搜索条件
      applyDialogFormVisible: false,
      leaveTime: '',
      rules: {
      },
      leaveList: [],
      loading: false,
      total: 0,
      beginTime:null,
      endTime:null,
      dialogType: false //dialog类型 false为申请 true为查看撤销
    };
  },
  methods: {
    submitForm () {
      // 在这里处理提交逻辑，例如发送请求到后端服务器保存请假申请数据。  
      // 可以在这里添加验证、处理表单数据等操作。  
      console.log('请假申请提交:', this.leaveTime, this.form);
      if (this.leaveTime == '') {
        this.$modal.msgError("请选择请假时间");
      }
      if (this.form.reason == '') {
        this.$modal.msgError("请输入请假原因");
      }
      if (this.form.type == '') {
        this.$modal.msgError("请选择请假类型");
      }
      this.form.beginTime = this.leaveTime[0]
      this.form.endTime = this.leaveTime[1]
      apply(this.form).then(res => {
        this.$modal.msgSuccess("申请成功");
        this.applyDialogFormVisible = false
        this.getList()
        this.resetForm()
      })
      // console.log(this.leaveTime)
    },
    /** 重置按钮操作 */
    resetQuery () {
      // this.resetForm("queryForm");
      this.resetQueryParams()
      this.beginTime = null
      this.endTime = null
      this.handleQuery();
    },
    resetQueryParams(){
        this.queryParams = {
          pageNum: 1,
        pageSize: 10,
        counsellorName: null,
        beginTime: null,
        beginStartTime:null,
        beginStopTime:null,
        endTime: null,
        endStartTime: null,
        endStopTime: null,
        status: null,
        ratifyTime: null,
        type: null,
        }
    },
    handleClose () {
      this.resetForm()
      this.applyDialogFormVisible = false
    },
    resetForm () {
      this.form = {
        beginTime: '',
        endTime: '',
        reason: '',
        type: null,
      }

      this.leaveTime = ''
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      if(this.beginTime){
        this.queryParams.beginStartTime = this.beginTime[0]
        this.queryParams.beginStopTime = this.beginTime[1]
      }else{
        this.queryParams.beginStartTime = null
        this.queryParams.beginStopTime = null
      }
      if(this.endTime){
        this.queryParams.endStartTime = this.endTime[0]
        this.queryParams.endStopTime = this.endTime[1]
      }else{
        this.queryParams.endStartTime = null
        this.queryParams.endStopTime = null
      }
      this.getList();
    },
    //查看
    handleShowDetail (row) {
      this.dialogType = true
      this.applyDialogFormVisible = true;
      this.form = row
      this.form.type = row.type + ''
      this.form.status = row.status + ''
      this.leaveTime = [this.form.beginTime, this.form.endTime]
    },
    //审批
    handleAppreve (askLeaveId, status) {
      approve({ askLeaveId, status }).then(res => {
        this.$modal.msgSuccess("审批成功")
        this.getList()
        this.applyDialogFormVisible = false
      })
    },
    handleApply () {
      this.applyDialogFormVisible = true
      this.dialogType = false
    },
    getList () {
      this.loading = true;
      listCounsellor(this.queryParams).then(response => {
        this.leaveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
  },
  created () {
    this.getList()
  }
};
  </script>  
    
  <style scoped>
.leave-request-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
.el-header {
  background-color: #f5f7fa;
  text-align: center;
  padding: 10px;
}
.el-main {
  padding: 20px;
}
.el-form {
  margin-top: 20px;
}
.el-form label {
  display: block;
  margin-bottom: 10px;
}
.el-form input,
.el-form select,
.el-form textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
/* .el-button {
  display: block;
  width: 100%;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  background-color: #4caf50; 
  color: white;
  margin: 10px 0;
  border: none;
  border-radius: 4px;
} */
</style>